package org.lanyu.springainovel.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.lanyu.springainovel.novel.entity.Inspiration;
import org.lanyu.springainovel.novel.mapper.InspirationMapper;
import org.lanyu.springainovel.novel.service.InspirationService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 灵感服务实现类
 */
@Slf4j
@Service
public class InspirationServiceImpl extends ServiceImpl<InspirationMapper, Inspiration> implements InspirationService {

    @Override
    public List<Inspiration> getInspirationsByUserId(Long userId) {
        QueryWrapper<Inspiration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("rating",  "update_time");
        return this.list(queryWrapper);
    }

    @Override
    public List<Inspiration> getInspirationsByBookId(Long bookId) {
        QueryWrapper<Inspiration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id", bookId);
        queryWrapper.orderByDesc("rating",  "update_time");
        return this.list(queryWrapper);
    }

    @Override
    public Inspiration createInspiration(Inspiration inspiration) {
        inspiration.setCreateTime(LocalDateTime.now());
        inspiration.setUpdateTime(LocalDateTime.now());
        // 默认为私有
        if (inspiration.getIsPublic() == null) {
            inspiration.setIsPublic(0);
        }
        this.save(inspiration);
        return inspiration;
    }

    @Override
    public Inspiration updateInspiration(Inspiration inspiration) {
        inspiration.setUpdateTime(LocalDateTime.now());
        this.updateById(inspiration);
        return inspiration;
    }

    @Override
    public Inspiration getInspirationById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<Inspiration> getInspirationsByTags(String tags) {
        return baseMapper.findByTags(tags);
    }

    @Override
    public List<Inspiration> getInspirationsByUserIdAndTags(Long userId, String tags) {
        return baseMapper.findByUserIdAndTags(userId, tags);
    }

    @Override
    public List<Inspiration> getHotInspirations(int limit) {
        return baseMapper.findHotInspirations(limit);
    }

}